package com.idp.common.base;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.idp.common.constant.SessionAttr;
import com.idp.common.constants.Constants;
import com.idp.common.interceptor.DateConvertEditor;
import com.idp.common.util.ContextHolderUtil;
import com.idp.web.system.entity.SysUser;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 基础controller
 *
 * @author King
 */
public class BaseController {

    /**
     * 将前台传递过来的日期格式的字符串，自动转化为Date类型
     *
     * @param binder
     */
    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {

        binder.registerCustomEditor(Date.class, new DateConvertEditor());
    }

    /**
     * 抽取由逗号分隔的主键列表
     *
     * @param ids
     * @return
     */
    protected List<String> extractIdListByComma(String ids) {
        List<String> result = new ArrayList<String>();
        if (StringUtils.hasText(ids)) {
            for (String id : ids.split(",")) {
                if (StringUtils.hasLength(id)) {
                    result.add(id.trim());
                }
            }
        }

        return result;
    }

    /**
     * 获取登录的当前用户信息
     *
     * @return
     */
    public SysUser getCurrentUser() {

        HttpSession session = ContextHolderUtil.getSession();
        SysUser user = (SysUser) session.getAttribute(SessionAttr.USER_LOGIN.getValue());

        return user;
    }

    /**
     * 获取userId
     *
     * @return
     */
    public String getUserId() {
        return getCurrentUser().getId();
    }

    /**
     * 获取userType
     *
     * @return
     */
    public Integer getUserType() {
        return getCurrentUser().getUserType();
    }

    private BaseResult baseResult;

    public BaseResult sendResult200() {
        return baseResult = new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200);
    }

    public BaseResult sendResult200(Object data) {
        return baseResult = new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, data);
    }

    public BaseResult sendResult400() {
        return baseResult = new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
    }

    public BaseResult sendResult400(Object data) {
        return baseResult = new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, data);
    }

    public BaseResult sendResult500() {
        return baseResult = new BaseResult(Constants.RESPONSE_CODE_500, Constants.RESPONSE_MESSAGE_500);
    }

    public BaseResult sendResult500(Object data) {
        return baseResult = new BaseResult(Constants.RESPONSE_CODE_500, Constants.RESPONSE_MESSAGE_500, data);
    }

    public BaseResult sendResult(int code, String message) {
        return baseResult = new BaseResult(code, message);
    }

    public BaseResult sendResult(int code, String message, Object data) {
        return baseResult = new BaseResult(code, message, data);
    }

    public BaseResult getBaseResult() {
        return baseResult;
    }

    public void setBaseResult(BaseResult baseResult) {
        this.baseResult = baseResult;
    }
}
